package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper mapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    public boolean checkPhone(String phone) {
        return mapper.selectCountByPhone(phone)>0;
    }

    public void sendVerifyCode(String phone) {
        //创建验证码
        String code = UUID.randomUUID().toString().substring(0, 4);

        //把验证码保存到Redis
        userInfoRedisService.setVerifyCode(phone,code);

        //发送手机验证码
        StringBuilder builder = new StringBuilder();
        builder.append("您的验证码是:").append(code).append(",请在").append(Consts.USER_INFO_TOKEN_VAI_TIME).append("内使用");

        //假装已经发送了,给前台注册使用
        System.out.println(builder.toString());
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //校验参数是否为空
        AssertUtil.hasLength(phone,"电话号码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"再次密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");

        //校验密码是否相等
        AssertUtil.isEquals(password,rpassword,"两次密码不一致");
        //校验手机号是否存在
        if(mapper.selectCountByPhone(phone)>0){
            throw new LogicException("手机号已被注册");
        }
        //校验验证码是否正确
        String code = userInfoRedisService.getVerifyCode(phone);
        System.out.println(code);
        if(!verifyCode.equals(code)){
            throw new LogicException("验证码不正确或者已过期");
        }

        //密码加密
        password = password + phone;
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());

        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setNickname(nickname);
        userInfo.setPassword(md5Password);

        userInfo.setLevel(1);  //用户级别
        userInfo.setState(UserInfo.STATE_NORMAL);

        userInfo.setHeadImgUrl("/images/default.jpg");
        mapper.insert(userInfo);
    }

    @Override
    public List<UserInfo> list() {
        return mapper.selectAll();
    }

    @Override
    public String userLogin(String username, String password) {
        //首先查询判断这个账户是否存在
        UserInfo userInfo = mapper.selectUserByUsernameAndPassword(username,password);

        //不存在的话抛出异常
        if(userInfo==null){
            throw new LogicException("用户名或者密码错误");
        }

        //存在的话创建一个token
        String token = UUID.randomUUID().toString().replace("-", "");

        //把token作为key,用户作为value存进Redis里面
        userInfoRedisService.addUserInfo(token,userInfo);

        //把token返回去给存进cookie使用
        return token;
    }
}
